Scenario based problems
(0478 May-June 2023 Scenario Questions)


Objectives : Student should be able to -


Q1.  A two-dimensional (2D) array Account[ ] contains account holders' names and passwords for a banking program.

A 2D array AccDetails[ ] has three columns containing the following details :

The amount of money in a bank account can be negative (overdrawn) but not by more than the overdraft limit.
For example, an account with an overdraft limit of 100.00 must have a balance that is greater than or equal to -100.00

Suitable error message must be displayed if a withdrawal cannot take place, for example if the overdraft limit or the size of withdrawal is exceeded.

The bank account ID gives the index of each account holder’s data held in the two arrays. For example, account ID 20’s details would be held in :
Account[20, 1] and Account[20, 2]
AccDetails[20, 1], AccDetails[20, 2] and AccDetails[20, 3]

The variable Size contains the number of accounts.

The arrays and variable Size have already been set up and the data stored.

Write a program that meets the following requirements:

You must use pseudocode or program code and add comments to explain how your code works. All inputs and outputs must contain suitable message.

You only need to declare any local arrays and local variables that you use.

You do not need to declare and initialise the data in the global arrays Account[ ] and AccDetails[ ] and the variable Size.


// Declaring global variables.
DECLARE  AccNumber,  Choice  :  INTEGER
DECLARE  ValidID,  Quit  :  BOOLEAN

// Ask to input the account number
OUTPUT  “Enter your account number : ”
INPUT  AccNumber
// Call the Procedure to check if the account number, user name and password is valid or not
CheckAccID(AccNumber)

IF  ValidID = TRUE  THEN
REPEAT
OUTPUT  “MENU”
OUTPUT  “1.  Display Balance”
OUTPUT  “2.  Withdraw money”
OUTPUT  “3.  Deposit money”
OUTPUT  “4.  Exit”

OUTPUT  “Chose any option from 1, 2, 3 or 4 :”
INPUT  Choice

// Call appropriate Procedure for user's choice
CASE  Choice  OF
1 :  Balance(AccNumber)
2 :  Withdraw(AccNumber)
3 :  Deposit(AccNumber)
4 :  Quit ‹— TRUE
OTHERWISE
OUTPUT  “Invalid choice, try again.”
ENDCASE
UNTIL  Choice = 4
ELSE
OUTPUT  “Access denied, invalid account number.”
ENDIF

// Procedure to check the account number, user name and password is correct
PROCEDURE  CheckAccID(AccID : INTEGER)
// Declare local variables
DECLARE  UserName,  Password  :  STRING

IF  AcID < 0  OR  AcID > Size  THEN
ValidID ‹— FALSE
OUTPUT  “Invalid account number”
ELSE
OUTPUT  “Enter the username : ”
INPUT  UserName
OUTPUT  “Enter the password : ”
INPUT  Password

IF  Account[AcID, 1] = UserName  AND  Account[AcID, 2] = Password  THEN
ValidID ‹— TRUE
ELSE
ValidID ‹— FALSE
OUTPUT  “Invalid username or password”
ENDIF

ENDIF
ENDPROCEDURE

// Procedure to display the account balance
PROCEDURE  Balance(AccID : INTEGER)
OUTPUT  “Your account balance is ”,  AccDetails[AcID, 1]
ENDPROCEDURE

// Procedure to withdraw money
PROCEDURE  Withdraw(AccID : INTEGER)
// Declare local variables
DECLARE  Amount  :  REAL

REPEAT
OUTPUT  “Enter the amount to withdraw : ”
INPUT  Amount

IF  Amount > AccDetails[AcID, 3]  THEN
OUTPUT  “Amount is greater than withdrawal limit”
ENDIF

IF  Amount > AccDetails[AcID, 1] + AccDetails[AcID, 2]  THEN
OUTPUT  “Amount is greater than available case and overdraft limit”
ENDIF

IF  Amount < AccDetails[AcID, 3]
AND  Amount < = AccDetails[AcID, 1] + AccDetails[AcID, 2]
THEN
// Deduct the money from balance and store it in array
AccDetails[AcID, 1] ‹— AccDetails[AcID, 1] - Amount
ENDIF

UNTIL  Amount < AccDetails[AcID, 3]
AND  Amount < = AccDetails[AcID, 1] + AccDetails[AcID, 2]
AND  Amount < 0
ENDPROCEDURE

// Procedure to deposit money
PROCEDURE  Deposit(AccID : INTEGER)
// Declare local variables
DECLARE  Amount  :  REAL

REPEAT
OUTPUT  “Enter the amount to deposit : ”
INPUT  Amount

IF  Amount < = 0  THEN
OUTPUT  “Invalid, amount should be greater than 0.”
ENDIF

UNTIL  Amount > 0
// Add the money to the balance and store it in array
AccDetails[AcID, 1] ‹— AccDetails[AcID, 1] + Amount
ENDPROCEDURE

Q2.  A one-dimensional (1D) array Days[ ] contains the names of the days of the week. A two-dimensional (2D) array Readings[ ] is used to store 24 temperature readings, taken once an hour, for each of the seven days of the week. A 1D array AverageTemp[ ] is used to store the average temperature for each day of the week.

The position of any day’s data is the same in all three arrays. For example, if Wednesday is in index 4 of Days[ ], Wednesday’s temperature readings are in index 4 of Readings[ ] and Wednesday’s average temperature is in index 4 of AverageTemp[ ].

The temperature readings are in Celsius to one decimal place. Temperatures can only be from -20.00C to +50.00C inclusive.

Write an algorithm that meets the following requirements:

You must use pseudocode or program code and add comment to explain how your code works.

You do not need to declare any arrays, variables or constants; you may assume that this has already been done.

You will need to initialise and populate the array Days[ ] at the start of the program.

All inputs and outputs must contain suitable messages.


// Initial population of Days[ ] array with days of the week
Days[1] ‹— "Sunday"
Days[1] ‹— "Monday"
Days[1] ‹— "Tuesday"
Days[1] ‹— "Wednesday"
Days[1] ‹— "Thursday"
Days[1] ‹— "Friday"
Days[1] ‹— "Saturday"

// Input temperatures inside nested loop
FOR  WeekLoop ‹— 1 TO 7

// Initialise the variable to calculate total day temperature
TotalDayTemp ‹— 0
FOR  DayLoop ‹— 1 TO 24
OUTPUT  “Enter temperature ”, DayLoop, “ for “, Days[WeekLoop]
INPUT  Temp
// Validation of input temperature between -20 and +50 inclusive
WHILE  (Temp < -20 OR Temp > 50)  DO
OUTPUT  “Invalid temp, try again.”
INPUT  Temp
ENDWHILE
Readings[WeekLoop, DayLoop] ‹— ROUND(Temp, 1)
// Totalling of temperature of the day
TotalDayTemps ‹— TotalDayTemps + ROUND(Temp, 1)
NEXT  DayLoop
// Calculate and store the average temperature for each day
AverageTemp[WeekLoop] ‹— ROUND(Temp / 24,  1)
NEXT  WeekLoop

// Initialise the variable to calculate the overall total temperature of the week
TotalWeekTemp ‹— 0
// Outputs the temperature of each day in Celsius and Fahrenheit
FOR  WeekLoop ‹— 1 TO 7

OUTPUT  “The average temperature on ”, Days[WeekLoop], “ was ”,
AverageTemp[WeekLoop], “ Celsius and ”,
ROUND(AverageWeekTemp[WeekLoop] * 9 / 5 + 32,  1),
“ Fahrenheit”
TotalWeekTemp ‹— TotalWeekTemp + AverageTemp[WeekLoop]
NEXT  WeekLoop

// Calculate the average temperature of the week in celsius and fahrenheit
AvgWkCelsiusTemp ‹— ROUND(TotalWeekTemp / 7,  1)
AvgWkFahrenTemp ‹— ROUND(AvgWkCelsiusTemp * 9 / 5 + 32,  1)
OUTPUT  “The average temperature for the week was ”, AvgWkCelsiusTemp,
“ Celsius and ”, AvgWkFahrenTemp, “ Fahrenheit.”

Q3.  A two-dimensional (2D) array Contacts[ ] is used to store names and telephone numbers. All the data is stored as strings. The array must have the capacity to store 100 contacts in the form of :

The variable CurrentSize shows how may contacts are in the array.

Write an algorithm that meets the following requirements:

You must use pseudocode or program code and add comment to explain how your code works.

All inputs and outputs must contain suitable message.

You do not need to initialise the data in the array Contacts[ ] and the variable CurrentSize.


// Allow the program to continue as long as user wants
Continue ‹— TRUE
WHILE  Continue = TRUE  DO
// Display the menu for the input of choice
OUTPUT  “Please choose one of the following :”
OUTPUT  “Press 1 :  to enter new contacts”
OUTPUT  “Press 2 :  to display your contacts”
OUTPUT  “Press 3 :  to delete all contacts”

// Validate the input of choice between 1 to 3
INPUT  Choice
WHILE   Choice < 1  OR  Choice > 3  DO
OUTPUT  “Invalid choice, try again (1, 2 or 3).”
INPUT  Choice
ENDWHILE

// Validate the input of choice 1 to check if the storage capacity of contact is full
WHILE   Choice = 1  AND  CurrentSize = 100  DO
OUTPUT  “Sorry, your contact storing capacity is full. Select 2 or 3.”
INPUT  Choice
ENDWHILE

CASE   Choice  OF
// For choice of 1, add new contacts
1 :  OUTPUT “How may contacts (maximum 5 only) ?”
INPUT  NoNewContacts
// Validate input for the maximum number of allowed new contacts
WHILE  NoNewContacts < 1 OR NoNewContacts > 5  DO
OUTPUT  “Invalid, only maximum of 5 new contacts is allowed.”
INPUT  NoNewContacts
ENDWHILE

// Check the space left for new contacts
WHILE  CurrentSize + NoNewContacts > 100  DO
OUTPUT  “Sorry, no space.”
OUTPUT  “You could only add ”,  100 – CurrentSize,  “ new contacts.”
INPUT  NoNewContacts
ENDWHILE

// Add new contacts
FOR   BlankRow ‹— CurrentSize + 1  TO  CurrentSize + NoNewContacts
OUTPUT  “Enter the first name followed by last name - ”
INPUT  Contacts[BlankRow,  1]
OUTPUT  “Enter the phone number - ”
INPUT  Contacts[BlankRow,  2]
NEXT   BlankRow

// Update the current size of the contacts
CurrentSize ‹— CurrentSize + NoNewContacts

// For choice of 2, Bubble Sort the contacts by name and display the records
2 :  IF  CurrentSize >= 2  THEN
REPEAT
Swap ‹— FALSE
FOR  Count ‹— 1 TO CurrentSize – 1
IF  Contacts[Count + 1, 1] < Contacts[Count, 1]  THEN
TempName ‹— Contacts[Count, 1]
TempPhone ‹— Contacts[Count, 2]
Contacts[Count, 1] ‹— Contacts[Count + 1, 1]
Contacts[Count, 2] ‹— Contacts[Count + 1, 2]
Contacts[Count + 1, 1] ‹— TempName
Contacts[Count + 1, 2] ‹— TempPhone
Swap ‹— TRUE
ENDIF
NEXT  Count
UNTIL  Swap ‹— FALSE
ENDIF

// Display sorted contact details
IF  CurrentSize > 0  THEN
OUTPUT  “Name and Telephone number”
FOR  Count ‹— 1 TO CurrentSize
OUTPUT  Contacts[Count, 1],  “ - ”,  Contacts[Count, 2]
NEXT  Count

// For choice of 3, Delete all contacts details
3 :  FOR  Row ‹— 1 TO CurrentSize
FOR  Col ‹— 1 TO 2
Contacts[Row, Col] ‹—  “  ”
NEXT  Col
NEXT  Row
OUTPUT  “All contact details are removed.”

ENDCASE

// Ask user whether to continue to use the program, yes or no.
OUTPUT  “Do you want to continue (Y / N) ?”
INPUT  WantContinue
IF  WantContinue = "N" OR WantContinue = "n"  THEN
Continue ‹— FALSE
ENDIF

ENDWHILE




* * * * * * * * *
* * * * * *
* * *
*